package com.web.model;

import java.io.Serializable;
import java.util.List;

import com.github.pagehelper.Page;

/**
 * 分页结果集实体
 * 
 * @author Administrator
 * 
 * @param <T>
 */
public class PageBean<T> implements Serializable {
	private static final long serialVersionUID = 8656597559014685635L;
	private long total; // 总记录数
	private List<T> list; // 结果集
	private int pageNum; // 第几页
	private int pageSize; // 每页记录数
	private int pages; // 总页数
	private String pageStr; // 最终页面显示的底部翻页导航，详细见：getPageStr();
	private String url;

	/**
	 * 包装Page对象，因为直接返回Page对象，在JSON处理以及其他情况下会被当成List来处理， 而出现一些问题。
	 * 
	 * @param list
	 *            page结果
	 * @param navigatePages
	 *            页码数量
	 */
	public PageBean(List<T> list) {
		if (list instanceof Page) {
			Page<T> page = (Page<T>) list;
			this.pageNum = page.getPageNum();
			this.pageSize = page.getPageSize();
			this.total = page.getTotal();
			this.pages = page.getPages();
			this.list = page;
		}
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public void setPageStr(String pageStr) {
		this.pageStr = pageStr;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getPageStr() {

		// <ul id="pagination-digg">
		// <li class="previous-off">&laquo;</li>
		// <li class="active">1</li>
		// <li><a href="?page=2">2</a></li>
		// <li><a href="?page=3">3</a></li>
		// <li><a href="?page=4">4</a></li>
		// <li><a href="?page=5">5</a></li>
		// <li><a href="?page=6">6</a></li>
		// <li><a href="?page=7">7</a></li>
		// <li class="next"><a href="?page=8">&raquo;</a></li>
		// </ul>

		StringBuffer sb = new StringBuffer();
		if (pages > 0) {
			sb.append("<ul id=\"pagination-digg\">");

			if (pageNum != 1) {
				sb.append("<li class=\"previous-off\"><a href=\"" + url
						+ (pageNum - 1) + "\">&laquo;</a></li>");
			}

			if (pageNum < 7) {
				int begin = 7;
				if (begin > pages) {
					begin = pages;
				}
				for (int i = 1; i <= begin; i++) {
					// 当前页号不需要超链接
					if (pageNum == i) {
						sb.append("<li class=\"active\"><a>" + i + "</a></li>");
					} else {
						sb.append("<li><a href=\"" + url + i + "\">" + i
								+ "</a></li>");
					}
				}
			} else {
				for (int i = pageNum - 3; i <= pageNum; i++) {
					// 当前页号不需要超链接
					if (pageNum == i) {
						sb.append("<li class=\"active\"><a>" + i + "</a></li>");
					} else {
						sb.append("<li><a href=\"" + url + i + "\">" + i
								+ "</a></li>");
					}
				}
				int end = pageNum + 2;
				if (end > pages) {
					end = pages;
				}
				for (int i = pageNum + 1; i <= end; i++) {
					sb.append("<li><a href=\"" + url + i + "\">" + i
							+ "</a></li>");
				}
			}
			if (pageNum != pages) {
				sb.append("<li class=\"next\"><a href=\"" + url + (pageNum + 1)
						+ "\">&raquo;</a></li>");
			}
			sb.append("</ul>");
		} else {
			sb.append("<strong>没有数据</strong>\r\n");
		}
		pageStr = sb.toString();
		return pageStr;
	}
}
